﻿/*
 * Project: IV:MP GUI Editor (http://code.google.com/p/ivmp-gui-editor)
 * Code license: GNU Lesser GPL (http://www.gnu.org/copyleft/lesser.html)
 * 
 * Authors: CrackHD
 * Description: 
 */

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace IvmpEditor
{
    public partial class GuiDesignerControl : GUIWindow, ITab
    {
        public readonly Control     Dummy;
        public readonly ETab        Tab;
        public GuiDesignerControl(ETab tab, string variableName)
            : base(null, variableName)
        {
            Tab = tab;
            Dummy = new Label();
            base.nativeControl = Workbench;
            IsSaved = false;

            // Init workbench:
            Workbench.Style = App.Current.Resources["CEGUI_Window_Editor"] as Style;
            Workbench.ContextMenu = tab.Editor.MainWindow.Resources["MenuWorkbench"] as ContextMenu;
        }

        // Methods:
        public void InitElement(GUIElement element)
        {
            // Initialize newly-created element
            if (element.GetName() == null)
            {
                // Variable name isn't set before, we generate a new name for this element.
                string newName = CreateVariableName(element.GetDefaultVarName());
                element.SetName(newName);
                element.SetText(newName, true);
            }
            element.SyncFromNative();

            // Events:
            element.MoveControlAgent.AgentMovedControl += MoveControlAgent_AgentMovedControl;
            element.NativeControl.PreviewMouseRightButtonDown += NativeControl_PreviewMouseRightButtonDown;
 
            element.NativeControl.ContextMenu = Tab.Editor.MainWindow.Resources["MenuElement"] as ContextMenu;
            element.SyncToNative();
            element.InstallMoveControlAgent();
            SetVirtualFocusOn(element);
        }
        public bool ContainsElement(string elementName)
        {
            // loop children elements
            foreach (GUIElement elem in this.GetChildren())
                if (elem.GetName() == elementName)
                    return true;

            return false;
        }
        public string CreateVariableName(string prefix)
        {
            // Create variable name based on prefix and number after it
            int i = 1;
            while (ContainsElement(prefix + i))
                i++;

            return prefix + i;
        }
        public void SetElementZIndex(GUIElement elem, int z)
        {
            if (z < 0)
                z = GetChildrenCount() - 1;

            // Ensure children elements stored in order equal to their Z-indexes
            // Re-adding element to Wokrbench's children will cause change of its Z-index..
            base.containerElements.Remove(elem);
            Workbench.Children.Remove(elem.NativeControl);
            base.containerElements.Insert(z, elem);
            Workbench.Children.Insert(z, elem.NativeControl);
            //elem.NativeControl.SetValue(Canvas.ZIndexProperty, z);
        }

        // Events:
        void NativeControl_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            // Set a virtual focus on this element (which RMB clicked)
            this.SetVirtualFocusOn(this.ChildGetByControl(sender as Control));
        }
        void MoveControlAgent_AgentMovedControl(Control ctrl, Point newPos)
        {
            var elem = this.ChildGetByControl(ctrl);
            if (elem == null)
                return; // this element is not in this window probably.

            elem.SyncFromNative();
        }

        // Properties:
        public bool IsSaved { get; set; }
        public bool IsDesiredNew { get; set; }
        string ITab.Name { get { return GetName(); } }

        // Methods:
        public void Focus() { }
        void ITab.SetupEvents() { }
        public bool Save()
        {
            throw new System.NotImplementedException();
        }
        public bool ShowSaveAsDialog()
        {
            throw new System.NotImplementedException();
        }
    }
    public partial class wndMain
    {
        // Workbench menu
        void MenuWorkbench_AddText_Click(object sender, RoutedEventArgs e)
        {
            var tab = CurrentTab;

            var elem = new GUIText(null);
            elem.SetParent(tab.GuiDesigner);
            tab.GuiDesigner.InitElement(elem);
        }
        void MenuWorkbench_AddButton_Click(object sender, RoutedEventArgs e)
        {
            var tab = CurrentTab;

            var elem = new GUIButton(null);
            elem.SetParent(tab.GuiDesigner);
            tab.GuiDesigner.InitElement(elem);
        }
        void MenuWorkbench_AddCheckBox_Click(object sender, RoutedEventArgs e)
        {
            var tab = CurrentTab;

            var elem = new GUICheckBox(null);
            elem.SetParent(tab.GuiDesigner);
            elem.PropertySet(CEGUI.ParseProperty("NormalTextColour;White"));
            elem.PropertySet(CEGUI.ParseProperty("NormalTextColour;Green"));
            elem.PropertySet(CEGUI.ParseProperty("NormalTextColour;White"));
            tab.GuiDesigner.InitElement(elem);
        }
        void MenuWorkbench_AddEditBox_Click(object sender, RoutedEventArgs e)
        {
            var tab = CurrentTab;

            var elem = new GUIEditBox(null);
            elem.SetParent(tab.GuiDesigner);
            tab.GuiDesigner.InitElement(elem);
        }
        void MenuWorkbench_GenerateCode_Click(object sender, RoutedEventArgs e)
        {
            var tab = CurrentTab;

            Clipboard.SetText(tab.GuiDesigner.GetSQCode().ToString());
            MessageBox.Show("GUI code copied to clipboard.");
        }

        // Element menu:
        void MenuElement_Remove_Click(object sender, RoutedEventArgs e)
        {
            var elem = CurrentTab.GuiDesigner.GetVirtualFocused();
            elem.MoveControlAgent.Uninstall();
            elem.SetParent(null);
        }
        void MenuElement_BringToFront_Click(object sender, RoutedEventArgs e)
        {
            var tab = CurrentTab;
            var elem = tab.GuiDesigner.GetVirtualFocused();
            tab.GuiDesigner.SetElementZIndex(elem, -1);
        }
        void MenuElement_SendToBack_Click(object sender, RoutedEventArgs e)
        {
            var tab = CurrentTab;
            var elem = tab.GuiDesigner.GetVirtualFocused();
            tab.GuiDesigner.SetElementZIndex(elem, 0);
        }
        void MenuElement_ViewSQCode_Click(object sender, RoutedEventArgs e)
        {
            var tab = CurrentTab;
            var elem = tab.GuiDesigner.GetVirtualFocused();

            elem.SyncFromNative();
            var sq = new SquirrelCodeStack();
            elem.GetSQCode(ref sq);
            string code = sq.ToString();
            Clipboard.SetText(code);
            MessageBox.Show(code + "\r\n\r\nCopied to your clipboard...", elem.GetTypeName() + ": element #" + tab.GuiDesigner.ChildIdByName(elem.GetName()));
        }
    }
}